using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Specialized;

using Db4o.Framework;
using Db4o.Framework.Business;
using Db4o.Framework.Query;
using Business;

using BusinessSite.Utility;


namespace BusinessSite.Controls
{
    public sealed class BusinessControl
    {
        #region Full lazy singleton pattern BusinessControl

        BusinessControl()
        {
        }

        public static BusinessControl Instance
        {
            get
            {
                return Nested.instance;
            }
        }

        class Nested
        {
            // Explicit static constructor to tell C# compiler
            // not to mark type as beforefieldinit
            static Nested()
            {
            }

            internal static readonly BusinessControl instance = new BusinessControl();
        }

        #endregion

        private static bool connected;

        public bool Connected
        {
            get
            {
                LoadDb4oConnexion();
                return connected;
            }

        }
        private static void LoadDb4oConnexion()
        {
            try
            {
                CoreInitializer.Instance.Configure("C:\\Inetpub\\development\\db4o.framework\\BusinessSite\\Bin\\Db4o.Framework.config");
                CoreInitializer.Instance.Load();
                connected = true;
            }
            catch (Exception)
            {
                connected = false;
            }
        }
        public void StoreObject(HttpRequest request,Type type, long id)
        {
            if (Connected)
            {
                ManagedType obj;
                if(id>0)
                    obj = DataManager.Instance.RetreiveById<ManagedType>(id);
                else
                    obj = BusinessFactory.Instance.CreateInstanceWithId(type);

                switch (type.Name)
                {
                    case "Product":

                        Product product = (Product)obj;
                        product.ProductName = request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtProductName"];
                        product.QuantityPerUnit = Int32.Parse(request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtQuantityPerUnit"]);
                        product.ReorderLevel = Int32.Parse(request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtReorderLevel"]);
                        product.UnitPrice = Int32.Parse(request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtUnitPrice"]);
                        product.UnitInStock = Int32.Parse(request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtUnitsInStock"]);
                        product.UnitsOnOrder = Int32.Parse(request.Form[WebPartsRenderer.MASTER_CONTENT_ID + "txtUnitsOnOrder"]);

                        DataManager.Instance.Store(product);

                        break;
                }

               
            }
            else
            {
                
            }
        }
    } 
}
